package puj.promobil.prometer.persistence;

import java.util.ArrayList;


import puj.promobil.prometer.dto.*;
import java.util.List;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
//import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	
	private static final String DATABASE_NAME = "prometer.sqlite";

    private static final int DATABASE_VERSION = 1;
    
    private Dao<Producto, Integer> productoDao = null;
    
    private Dao<Empresa, Integer> empresaDao = null;
    
    private Dao<Imagen, Integer> imagenDao = null;

    private Dao<Configuracion, Integer> configuracionDao = null;

    
    public DatabaseHelper(Context context) {
    	super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	

	@Override
	 public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource) {
        try {
        	TableUtils.createTable(connectionSource, Empresa.class);
            TableUtils.createTable(connectionSource, Producto.class);
            TableUtils.createTable(connectionSource, Imagen.class);
            TableUtils.createTable(connectionSource, Configuracion.class);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        
    }

	@Override
	 public void onUpgrade(SQLiteDatabase db,ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            List<String> allSql = new ArrayList<String>(); 
            switch(oldVersion) 
            {
              case 1: 
                  //don't doit
            }
            for (String sql : allSql) {
                db.execSQL(sql);
            }
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "exception during onUpgrade", e);
            throw new RuntimeException(e);
        }
        
    }
	
	public Dao<Producto, Integer> getProductoDao() {
        if (null == productoDao) {
            try {
                productoDao = getDao(Producto.class);
            }catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return productoDao;
    }
	
	
	public Dao<Empresa, Integer> getEmpresaDao() {
        if (null == empresaDao) {
            try {
            	empresaDao = getDao(Empresa.class);
            }catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return empresaDao;
    }
	
	
	public Dao<Imagen, Integer> getImagenDao() {
        if (null == imagenDao) {
            try {
            	imagenDao = getDao(Imagen.class);
            }catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return imagenDao;
    }
	
	
	public Dao<Configuracion, Integer> getConfiguracionDao() {
        if (null == configuracionDao) {
            try {
            	configuracionDao = getDao(Configuracion.class);
            }catch (java.sql.SQLException e) {
                e.printStackTrace();
            }
        }
        return configuracionDao;
    }
}
